﻿using System.Collections.Generic;
using System.Linq;
using UnityEngine;

namespace Data
{
    public static class Utils
    {
        private static readonly List<WeightedValue> LootValues = new()
        {
            new WeightedValue { Value = "exit", Weight = 5 },
            new WeightedValue { Value = "gun", Weight = 20 },
            new WeightedValue { Value = "life", Weight = 30 },
            new WeightedValue { Value = "ammo", Weight = 50 },
            new WeightedValue { Value = "empty", Weight = 300 },
            new WeightedValue { Value = "bomb", Weight = 500 }
        };

        public static void Quit()
        {
#if (UNITY_EDITOR)
            UnityEditor.EditorApplication.isPlaying = false;
#elif (UNITY_STANDALONE)
            Application.Quit();
#endif
        }

        public static string GetRandomLootWeightedValue()
        {
            var totalWeight = LootValues.Sum(item => item.Weight);
            var randomWeight = Random.Range(0, totalWeight);

            foreach (var item in LootValues)
            {
                if (randomWeight < item.Weight)
                    return item.Value;
                randomWeight -= item.Weight;
            }

            return LootValues[^1].Value;
        }

        public static int GetRandomIndex(int count) =>
            Random.Range(0, count);

        public static void Cleanup<T>(List<T> list) where T : MonoBehaviour
        {
            foreach (var item in list)
                Object.Destroy(item.gameObject);
            list.Clear();
        }

        public static void Cleanup(List<GameObject> list)
        {
            foreach (var item in list)
                Object.Destroy(item);
            list.Clear();
        }
    }
}